﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Linq;
using Common.Type;

namespace Warranty.Configuration
{
    public static class SystemConfiguration
    {
        private static string connectionString;
        public static string ConnectionString
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(connectionString)) return connectionString;
                connectionString = ConfigurationManager.AppSettings["ConnectionString"];
                if (string.IsNullOrWhiteSpace(connectionString)) return "DefaultConnection";
                return connectionString;
            }
        }

        private static ConvertType? namingType;
        public static ConvertType NamingType
        {
            get
            {
                if (namingType.HasValue) return namingType.Value;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x == "NamingType");
                string stringVal = string.Empty;
                if (!string.IsNullOrEmpty(key)) stringVal = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(stringVal))
                {
                    namingType = ConvertType.CAPITALIZED;
                    return ConvertType.CAPITALIZED;
                }
                stringVal = stringVal.ToUpper();

                ConvertType result = ConvertType.CAPITALIZED;
                if (Enum.TryParse<ConvertType>(stringVal, out result))
                {
                    namingType = result;
                    return result;
                }

                return result;
            }
        }

        private static string tablePrefix;
        public static string TABLE_PREFIX
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(tablePrefix)) return tablePrefix;
                var key = ConfigurationManager.AppSettings.AllKeys.FirstOrDefault(x => x.ToUpper() == "TABLE_PREFIX");
                if (!string.IsNullOrEmpty(key)) tablePrefix = ConfigurationManager.AppSettings[key];
                if (string.IsNullOrWhiteSpace(tablePrefix)) return string.Empty;
                tablePrefix = tablePrefix.ToUpper();
                return tablePrefix;
            }
        }
    }
}
